var kl8Ai = function() {
    this.arr = ['一', '二', '三', '四', '五', '六', '七', '八'];
    ai.call(this);
};
kl8Ai.prototype = Object.create(ai.prototype);

// 记录到受影响的数字
kl8Ai.prototype.insertToNumber = function(list) {
    // 记录到各受影响的号码
    var self = this;
    var result = [];
    for (var i = 0; i < 20; i++) {
        result[i] = {
            index: i + 1,
            total: 0,
            bet: [],
        };
    }
    for (var i in list) {
        var k = list[i].key;
        if (k.startsWith("和") && k != '和810' || k.startsWith("正:")) {
            for (var j = 0; j < 20; j++) {
                result[j].bet.push(k);
                result[j].total += list[i].amount;
            }
        }
    }
    result.sort(function(a, b) {
        return b.total - a.total;
    });
    return result;
}

// 检测某个投注项，并应用概率
kl8Ai.prototype.apply = function(key, key2, arr1, arr2, all) {
    var self = this;
    if (key != key2) return null;
    if (this.checkRatio(self.rate)) {
        all = self.prior(all, arr1);
    }
    else {
        all = self.prior(all, arr2);
    }
    return all;
}

// 应用总和的概率
kl8Ai.prototype.he = function(result, index, he, he2) {
    var self = this;
    for (var i in result) {
        var data = result[i];
        self.deleteItem(data.bet, he);

        if (he2 == '和大' || he2 == '和小') {
            if (!data.pre) data.pre = [he2];
            else data.pre.push(he2);
        }
    }
    if (he2 == '和单' || he2 == '和双' || he2.startsWith('和大单') || he2.startsWith('和大双') ||
        he2.startsWith('和小单') || he2.startsWith('和小双')) {
        var data = result[19];
        if (!data.pre) data.pre = [he2];
        else data.pre.push(he2);
    }
    
    var arr = [];
    if (he2 == '和大') {
        arr = self.range(50, 80);
    }
    else if (he2 == '和小') {
        arr = self.range(1, 30);
    }
    else {
        arr = self.range(1, 80);
    }
    return arr;
}

// 取得总和的开奖数据
kl8Ai.prototype.heNumbers = function(index, he, ns) {
    var total = 0;
    var arr = [];
    var unopen = 0;
    for (var i = 0; i < 20; i++) {
        if (ns[i] === undefined) {
            unopen++;
            continue;
        }
        total += ns[i];
    }

    if (he == '和单' && total % 2 == 0 || he == '和双' && total % 2 == 1 ||
        he == '和大单' && total % 2 == 0 || he == '和小单' && total % 2 == 0 ||
        he == '和大双' && total % 2 == 1 || he == '和小双' && total % 2 == 1 ||
        he == '和大单X' && total % 2 == 1 || he == '和小单X' && total % 2 == 1 ||
        he == '和大双X' && total % 2 == 0 || he == '和小双X' && total % 2 == 0) {
        for (var i = 1; i <= 80; i += 2) {
            arr.push(i);
        }
        return arr;
    }
    if (he == '和单' && total % 2 == 1 || he == '和双' && total % 2 == 0 ||
        he == '和大单' && total % 2 == 1 || he == '和小单' && total % 2 == 1 ||
        he == '和大双' && total % 2 == 0 || he == '和小双' && total % 2 == 0 ||
        he == '和大单X' && total % 2 == 0 || he == '和小单X' && total % 2 == 0 ||
        he == '和大双X' && total % 2 == 1 || he == '和小双X' && total % 2 == 1) {
        for (var i = 2; i <= 80; i += 2) {
            arr.push(i);
        }
        return arr;
    }

    if (unopen > 1) {
        if (he == '和大') {
            for (var i = 30; i <= 89; i++) arr.push(i);
        }
        else {
            for (var i = 1; i <= 50; i++) arr.push(i);
        }
    }
    else {
        for (var i = 1; i <= 80; i++) {
            if (he == '和大' && total + i > 810) arr.push(i);
            else if (he == '和小' && total + i < 810) arr.push(i);
        }
    }
    return arr;
}

// 排除掉list的数字
kl8Ai.prototype.exclude = function(list) {
    var arr = [];
    for (var i = 1; i <= 80; i++) {
        if (list.indexOf(i) == -1) arr.push(i);
    }
    return arr;
}

// 入口
kl8Ai.prototype.open = function(bets, lottery, rate) {
    var self = this;
    self.map = lottery;
    self.rate = rate;
    
    // 合并下数据
    var arr = [["和大", "和小"], ["和单", "和双"], ["和尾大", "和尾小"]];
    var list = self.mixBets(bets, arr);

    // 记录到各受影响的号码
    var result = self.insertToNumber(list);

    // 逐个号码开出来
    var numbers = self.range(1, 80);
    var lottery = [];
    var ns = [];
    for (var i in result) {
        var index = result[i].index;
        var bets = result[i].bet;
        if (self.isEmpty(bets) && self.isEmpty(result[i].pre)) continue;

        var all = numbers.concat([]);
        if (result[i].pre) {
            // 这些受其他号码影响，需要优先处理
            for (var j in result[i].pre) {
                var k = result[i].pre[j];
                if (k.startsWith("和")) {
                    all = self.prior(all, self.heNumbers(index, k, ns));
                }
            }
        }
        for (var j in bets) {
            var k = bets[j];
            if (!k) continue;
            var k2 = k.substring(1);
            do {
                if (k == '和大') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, self.he(result, index, k, '和小'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                    break;
                }
                else if (k == '和小') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, self.he(result, index, k, '和大'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                    break;
                }
                else if (k == '和单') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, self.he(result, index, k, '和双'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                    break;
                }
                else if (k == '和双') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, self.he(result, index, k, '和单'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                    break;
                }
                else if (k == '和小单') {
                    if (self.checkRatio(1 - (1 - rate) / (1 - rate + 3 * rate))) 
                        all = self.prior(all, self.he(result, index, k, '和小单X'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                    break;
                }
                else if (k == '和小双') {
                    if (self.checkRatio(1 - (1 - rate) / (1 - rate + 3 * rate))) 
                        all = self.prior(all, self.he(result, index, k, '和小双X'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                    break;
                }
                else if (k == '和大双') {
                    if (self.checkRatio(1 - (1 - rate) / (1 - rate + 3 * rate))) 
                        all = self.prior(all, self.he(result, index, k, '和大双X'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                    break;
                }
                else if (k == '和大单') {
                    if (self.checkRatio(1 - (1 - rate) / (1 - rate + 3 * rate))) 
                        all = self.prior(all, self.he(result, index, k, '和大单X'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                    break;
                }

                if (k.startsWith("正:")) {
                    if (self.checkRatio(1 - (1 - rate) / (1 - rate + 79 * rate))) {
                        all = self.prior(all, self.exclude([parseInt(k.substring(2))]));
                    }
                    else {
                        all = self.prior(all, [parseInt(k.substring(2))]);
                    }
                }
            } while (false);
            if (all.length == 1) break;
        }

        // 应用概率，抽取一个出来
        ns[index - 1] = self.random(all);
        self.deleteItem(numbers, ns[index - 1]);
    }

    // 还没有开出的号码，随机开了
    for (var i = 0; i < 20; i++) {
        if (ns[i] !== undefined) continue;

        ns[i] = self.random(numbers);
        self.deleteItem(numbers, ns[i]);
    }

    // 返回开奖结果
    return ns;
}
var Kl8Ai = new kl8Ai();
